/** @noSelfInFile **/
import {LocModel} from "../../../base/LocModel";
import {UnitModel} from "../../../adapterModel/model3/UnitModel";
import {IPlayerAdapter} from "../../adapterInterface/model4/IPlayerAdapter";
import {PlayerDataKey, PlayerSourceData} from "../../../../dataSource/DataSource";
import Configer from "../../../config/Configer";
import {ConfigKey} from "../../../../../constant/ConfigConstant";
import DebugCheckSyncUtil from "./util/debug/DebugCheckSyncUtil";


export class PlayerAdapter implements IPlayerAdapter {
    bindHandle(handle: any): void {
        handle_ref(handle);
    }

    clearSelected(handle: any): void {
        if (handle === GetLocalPlayer()) {
            ClearSelection();
        }
    }

    getLocalPlayerId(): number {
        return GetPlayerId(GetLocalPlayer());
    }

    consumMallItemCount(handle: any, key: string, num: number): boolean {
        return DzAPI_Map_ConsumeMallItem(handle, key, num)
    }

    coordsFogged(handle: any, loc: LocModel): boolean {
        return IsFoggedToPlayer(loc.x, loc.y, handle);
    }

    coordsMasked(handle: any, loc: LocModel): boolean {
        return IsMaskedToPlayer(loc.x, loc.y, handle);
    }

    coordsVisible(handle: any, loc: LocModel): boolean {
        return IsVisibleToPlayer(loc.x, loc.y, handle);
    }

    createHandle(index: number): any {
        return Player(index);
    }

    destroy(handle: any): void {
        handle_unref(handle);
    }

    displayText(handle: any, text: string): void {
        DisplayTextToPlayer(handle, 0, 0, text);
    }

    equal(handle1: any, handle2: any): boolean {
        return  (GetHandleId(handle1) === GetHandleId(handle2));
    }

    getAllFood(handle: any): number {
        return  GetPlayerState(handle, PLAYER_STATE_RESOURCE_FOOD_CAP);
    }

    getCameraX(handle: any): number {
        return  GetCameraTargetPositionX();
    }

    getCameraY(handle: any): number {
        return  GetCameraTargetPositionY();
    }

    getFakeRandom(handle: any, min: number, max: number, data: PlayerSourceData): number {
        max = max || 1;
        min = min || 0;
        // 根据数值进行随机值计算
        data[PlayerDataKey.seed] = (data[PlayerDataKey.seed] * 9301 + 49297) % 233280;
        let rnd = data[PlayerDataKey.seed] / 233280;

        return min + rnd * (max - min);
    }

    getGold(handle: any): number {
        return  GetPlayerState(handle, PLAYER_STATE_RESOURCE_GOLD);
    }

    getHasMallItem(handle: any, key: string): boolean {
        return DzAPI_Map_HasMallItem(handle, key);
    }

    getLumber(handle: any): number {
        return  GetPlayerState(handle, PLAYER_STATE_RESOURCE_LUMBER);
    }

    getMallItemCount(handle: any, key: string): number {
        return  DzAPI_Map_GetMallItemCount(handle, key);
    }

    getMapLevel(handle: any): number {
        return DzAPI_Map_GetMapLevel(handle);
    }

    getName(handle: any): string {
        return  GetPlayerName(handle);
    }

    getPlayerId(handle: any): number {
        return  GetPlayerId(handle);
    }

    getStartLoc(handle: any): LocModel {
        const location = GetPlayerStartLocationLoc(handle);
        const locModel = new LocModel(GetLocationX(location), GetLocationY(location));
        return locModel;
    }

    getStoredString(handle: any, key: string): string {
        return DzAPI_Map_GetServerValue(handle, key);
    }

    getTeam(handle: any): number {
        return  GetPlayerTeam(handle);
    }

    getTech(handle: any, id: string): number {
        return GetPlayerTechCountSimple(FourCC(id), handle)
    }

    getUsedFood(handle: any): number {
        return  GetPlayerState(handle, PLAYER_STATE_RESOURCE_FOOD_USED);
    }

    isLocalPlayer(handle: any): boolean {
        return GetLocalPlayer() === handle;
    }

    isPlayerAlly(handle: any, player: any): boolean {
        return  IsPlayerAlly(handle, player);
    }

    isPlaying(handle: any): boolean {
        const isPlaying = (GetPlayerController(handle) == MAP_CONTROL_USER && GetPlayerSlotState(handle) == PLAYER_SLOT_STATE_PLAYING);
        return isPlaying
    }

    isValid(handle: any): boolean {
        if (handle) {
            return true;
        }
        return false;
    }

    setCameraAngle(handle: any, angle: number): void {
        SetCameraField( CAMERA_FIELD_ANGLE_OF_ATTACK, angle, 0)
    }

    moveCamera(handle: any, loc: LocModel, height: number, duration: number): void {
        const location = Location(loc.x, loc.y);
        PanCameraToTimedLocWithZForPlayer(handle, location, height * 10, duration);
    }

    moveUnit(unit: UnitModel, loc: LocModel): void {
        if (unit?.isValid()) {
        } else {
            return;
        }
        SetUnitPosition(unit.handle, loc.x, loc.y);
    }

    removeAllGuardPositions(handle: any): void {
        RemoveAllGuardPositions(handle);
    }

    removeFog(handle: any, rect: any): void {
        CreateFogModifierRectBJ(true, handle, FOG_OF_WAR_VISIBLE, rect);
    }
    addFog(handle: any, fogmodifier: any): void {
        DestroyFogModifier(fogmodifier);
    }

    setAllFood(handle: any, value: number): void {
        SetPlayerState(handle, PLAYER_STATE_RESOURCE_FOOD_CAP, value);
    }

    setAlly(handle: any, player: any, bool: boolean): void {
        if (bool) {
            SetPlayerAllianceStateBJ(handle, handle, bj_ALLIANCE_ALLIED);
        } else {
            SetPlayerAllianceStateBJ(handle, handle, bj_ALLIANCE_UNALLIED);
        }
    }

    setCameraHeight(handle: any, height: number): void {
        SetCameraFieldForPlayer(handle, CAMERA_FIELD_TARGET_DISTANCE, height, 0);
    }

    setGold(handle: any, value: number): void {
        SetPlayerState(handle, PLAYER_STATE_RESOURCE_GOLD, value);
    }

    setLumber(handle: any, value: number): void {
        SetPlayerState(handle, PLAYER_STATE_RESOURCE_LUMBER, value);
    }

    setName(handle: any, value: string): void {
        SetPlayerName(handle, value);
    }

    setPlayerGoldReward(handle: any, bool: boolean): void {
        SetPlayerFlagBJ(PLAYER_STATE_GIVES_BOUNTY, bool, handle);
    }

    setRoomList(handle: any, key: string, value: string): void {
        return DzAPI_Map_Stat_SetStat(handle, key, value)
    }

    setStoredString(handle: any, key: string, value: string): void {
        DzAPI_Map_SaveServerValue(handle, key, value);
    }

    setTech(handle: any, id: string, lv: number): void {
        SetPlayerTechResearched(handle, id, lv);
    }

    setUsedFood(handle: any, value: number): void {
        SetPlayerState(handle, PLAYER_STATE_RESOURCE_FOOD_USED, value);
    }

    signal(handle: any, loc: LocModel): void {
        const taskTipDurition = Configer.getConfig(ConfigKey.signalDurition) as number;
        PingMinimapForPlayer(handle, loc.x, loc.y, taskTipDurition);
    }

    toBeUnitOwner(handle: any, unitModel: UnitModel, changeColor: boolean): void {
        if (unitModel.isValid()) {
        } else {
            return;
        }
        SetUnitOwner(unitModel.handle, handle, changeColor);
    }

    toLose(handle: any): void {
        CustomDefeatBJ(handle, '游戏失败');
    }

    toWin(handle: any): void {
        CustomVictoryBJ(handle, true, false);
    }

    startCheckSync() {
        DebugCheckSyncUtil.start();
    }
}
